﻿using Furion.DatabaseAccessor;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace JoyAdmin.Core
{
    /// <summary>
    /// 权限表
    /// </summary>
    public class Security : BusEntity, IEntitySeedData<Security>
    {
        /// <summary>
        /// 权限唯一名（每一个接口）
        /// </summary>
        [StringLength(50)]
        public string UniqueName { get; set; }
       
        /// <summary>
        /// 多对多
        /// </summary>
        public ICollection<Role> Roles { get; set; }

        /// <summary>
        /// 多对多中间表
        /// </summary>
        public List<RoleSecurity> RoleSecurities { get; set; }

        /// <summary>
        /// 种子数据
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="dbContextLocator"></param>
        /// <returns></returns>
        public IEnumerable<Security> HasData(DbContext dbContext, Type dbContextLocator)
        {
            var securities = typeof(SecurityConst).GetFields().Select(u => u.GetRawConstantValue().ToString()).ToArray();
            var list = new List<Security>
            {
                new Security { Id = 1, CreatedTime = DateTimeOffset.Now, UniqueName = SecurityConst.S_System, TenantId = Guid.Parse("6D29EF24-AF73-69E2-AD76-ADB95959CE45") },
                new Security { Id = 2, CreatedTime = DateTimeOffset.Now, UniqueName = SecurityConst.S_Auth, TenantId = Guid.Parse("6D29EF24-AF73-69E2-AD76-ADB95959CE45") },
                new Security { Id = 3, CreatedTime = DateTimeOffset.Now, UniqueName = SecurityConst.S_Role, TenantId = Guid.Parse("6D29EF24-AF73-69E2-AD76-ADB95959CE45") },
                new Security { Id = 4, CreatedTime = DateTimeOffset.Now, UniqueName = SecurityConst.S_Employee, TenantId = Guid.Parse("6D29EF24-AF73-69E2-AD76-ADB95959CE45") }
            };
            for (var i = 1; i < securities.Length; i++)
            { 
                list.Add(new Security { Id = (i+4), CreatedTime = DateTimeOffset.Now, UniqueName = securities[i], TenantId = Guid.Parse("383AFB88-F519-FFF8-B364-6D563BF3687F") });
            }

            return list;
        }
    }
}